import { Types } from 'mongoose';

// 基础服务接口
export interface BaseService {
  healthCheck(): Promise<boolean>;
  cleanup(): Promise<void>;
  stop?(): void;
}

// 试题难度相关类型
export interface DifficultyConfig {
  baseLevel: number;
  adjustmentFactor: number;
  minLevel: number;
  maxLevel: number;
  historyWeight: number;
}

// AI 评分相关类型
export interface ScoringConfig {
  model: string;
  weights: {
    accuracy: number;
    completeness: number;
    relevance: number;
    clarity: number;
  };
  thresholds: {
    minimum: number;
    passing: number;
    excellent: number;
  };
}

// 数据分析相关类型
export interface AnalyticsConfig {
  preloadThreshold: number;
  cacheStrategy: {
    ttl: number;
    maxSize: number;
    updateFrequency: number;
  };
  aggregationRules: {
    timeRanges: string[];
    dimensions: string[];
    metrics: string[];
  };
}

// 缓存配置类型
export interface CacheConfig {
  prefix: string;
  ttl: number;
  maxSize?: number;
  evictionPolicy?: 'lru' | 'lfu' | 'fifo';
}

// 服务接口定义
export interface QuestionService extends BaseService {
  adjustDifficulty(
    questionId: Types.ObjectId,
    stats: {
      correctRate: number;
      averageTime: number;
      attemptCount: number;
    }
  ): Promise<DifficultyAdjustment>;
  batchImport(config: ImportConfig, data: any[]): Promise<number>;
  batchExport(config: ExportConfig): Promise<string>;
}

export interface ScoringService extends BaseService {
  getCachedScore(answerId: Types.ObjectId): Promise<ScoringCache | null>;
  updateScoringConfig(config: Partial<ScoringConfig>): Promise<void>;
}

export interface AnalyticsService extends BaseService {
  preloadData(config: AnalyticsConfig): Promise<void>;
  getCachedAnalytics(key: string): Promise<AnalyticsCache | null>;
  invalidateCache(pattern: string): Promise<void>;
}

// 存储相关类型
export interface DifficultyAdjustment {
  questionId: Types.ObjectId;
  oldLevel: number;
  newLevel: number;
  adjustmentReason: string;
  timestamp: Date;
}

export interface ScoringCache {
  answerId: Types.ObjectId;
  score: number;
  breakdown: {
    accuracy: number;
    completeness: number;
    relevance: number;
    clarity: number;
  };
  model: string;
  timestamp: Date;
  expiresAt: Date;
}

export interface AnalyticsCache {
  key: string;
  data: unknown;
  createdAt: Date;
  expiresAt: Date;
  lastAccessed: Date;
  accessCount: number;
}

// 导入导出配置类型
export interface ImportConfig {
  format: 'csv' | 'xlsx' | 'json';
  batchSize: number;
  validationRules: Record<string, (value: any) => boolean>;
  fieldMappings: Record<string, string>;
}

export interface ExportConfig {
  format: 'csv' | 'xlsx' | 'json';
  filters: Record<string, any>;
  fields: string[];
  sorting?: Record<string, 1 | -1>;
}

// 监控相关类型
export interface HealthCheck {
  status: 'healthy' | 'unhealthy' | 'degraded';
  timestamp: Date;
  details?: Record<string, unknown>;
}

export interface MetricOptions {
  tags?: Record<string, string>;
  timestamp?: number;
  aggregation?: 'sum' | 'avg' | 'min' | 'max' | 'count';
}

export interface ServiceMetrics {
  recordMetric(name: string, value: number, options?: MetricOptions): void;
  getMetrics(pattern: string, timeRange?: { start: Date; end: Date }): Promise<Record<string, unknown>>;
}

// 错误处理相关类型
export interface ErrorDetails {
  code: string;
  message: string;
  stack?: string;
  context?: Record<string, unknown>;
}

export interface ErrorHandling {
  handleError(error: Error, context?: Record<string, unknown>): void;
  getLastErrors(limit?: number): Promise<ErrorDetails[]>;
}

// 缓存操作相关类型
export interface CacheOperations {
  get<T>(key: string): Promise<T | null>;
  set<T>(key: string, value: T, ttl?: number): Promise<void>;
  del(key: string | string[]): Promise<void>;
  exists(key: string): Promise<boolean>;
  clear(pattern?: string): Promise<void>;
}
